package com.pro.xwt.rest.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.model.DFile;
import com.pro.xwt.common.model.DFolder;
import com.pro.xwt.common.service.BProjectService;
import com.pro.xwt.common.service.DFileService;
import com.pro.xwt.common.service.DFolderService;
import com.pro.xwt.common.utils.Common;
import com.pro.xwt.rest.ResponseResult;

@Controller
@RequestMapping("/folders")
public class DFolderController {
	@Autowired
	DFolderService dFolderService;
	@Autowired
	DFileService dFileService;
	@Autowired
	BProjectService bProjectService;

	/**
	 * 查询某个文件夹下的文件夹和文件的接口
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.GET }, value = "/{folderId}/files")
	@ResponseBody
	public ResponseResult get(@PathVariable String folderId) {

		Map<String, Object> map = new HashMap<>();

		List<DFile> dFiles = dFileService.findSubFileByParentId(folderId);
        List<DFolder> dFolders = dFolderService.findSubFoldByParentId(folderId);
		map.put("file", dFiles);
		map.put("folder", dFolders);
		return ResponseResult.success("查找成功", map);
	}

	/**
	 * 新建文件夹接口
	 * 
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.POST }, value = "")
	@ResponseBody
	public ResponseResult setFolder(@RequestBody DFolder dFolder, HttpServletRequest request) {
		
	/*	if (!isOwnerOrAdmin((BEmployee) request.getAttribute("user"), dFolder.getProjId())) {
			return ResponseResult.fail("你没有该权限", false);
		}*/
		Integer flag = 0;
		BEmployee user = (BEmployee) request.getAttribute("user");
		dFolder.setId(Common.generateUUID());
		dFolder.setSize((long) 0);
		dFolder.setCreateTime(new Date());
		dFolder.setUpdateTime(new Date());
		dFolder.setDeleteState("0");
		dFolder.setCreateEmpId(user.getId());
		dFolder.setCreateEmpName(user.getName());
		flag = dFolderService.insert(dFolder);
		if (flag > 0) {
			return ResponseResult.success("新建文件夹成功", dFolder);
		}
		return ResponseResult.fail("新建文件夹失败", null);
	}
	
	
	/**
	 * 修改文件夹名
	 * @param id
	 * @param dFile
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.PATCH }, value = "/{id}/projects/{projId}")
	@ResponseBody
	public ResponseResult update(@PathVariable String id,@PathVariable String projId,@RequestBody DFolder dFolder,HttpServletRequest request) {
		DFolder dF =dFolderService.findById(id);
		BEmployee user =(BEmployee) request.getAttribute("user");
		if (!isOwnerOrAdmin(user, dFolder.getProjId())&&!dF.getCreateEmpId().equals(user.getId())) {
			return ResponseResult.fail("你没有该权限", false);
		}
		dFolder.setId(id);
		int flag = dFolderService.update(dFolder);
		if(flag==1){
			DFolder dFo =dFolderService.findById(id);
			return ResponseResult.success("修改文件夹名成功", dFo);
		}
		return ResponseResult.fail("修改文件夹名失败", flag);
	}
	/**
	 * 文件夹删除
	 * @param id
	 * @param dFile
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.DELETE }, value = "/{id}/projects/{projId}")
	@ResponseBody
	public ResponseResult deleteFolder(@PathVariable String id,@PathVariable String projId,HttpServletRequest request) {
		DFolder dF =dFolderService.findById(id);
		BEmployee user =(BEmployee) request.getAttribute("user");
		if (!isOwnerOrAdmin(user, projId) && !dF.getCreateEmpId().equals(user.getId())) {
			return ResponseResult.fail("你没有该权限", false);
		}
		int flag = dFolderService.deleteFileAndFolder(id);
		if(flag==1){
			return ResponseResult.success("删除文件夹成功", flag);
		}
		return ResponseResult.fail("删除文件夹失败", flag);
	}
	
	
	
	
    public boolean isOwnerOrAdmin(BEmployee bEmployee, String projId) {
		if (bEmployee.getRole().equals("ADMIN")) {
			return true;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createEmpId", bEmployee.getId());
		map.put("id", projId);
		bProjectService.findByCondition(map);
		return (bProjectService.findByCondition(map) != null);
	}
	
}
